package me.minecraft.woodenman.command;

import me.minecraft.woodenman.GameStage;
import me.minecraft.woodenman.WoodenMan;
import me.minecraft.woodenman.config.Language;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.NotNull;

/**
 * @author langle__
 * @version 1.0
 */
public class JoinCommand implements CommandExecutor {

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(Language.ONLY_PLAYER_CAN_USE);
            return false;
        }
        Player player = (Player) sender;

        if (WoodenMan.gameTask == null) {
            player.sendMessage(Language.GAME_NOT_STARTED);
            return false;
        }
        GameStage gameStage = WoodenMan.gameTask.getGameStage();
        if (gameStage == GameStage.WAITING || gameStage == GameStage.PLAYING) {
            player.sendMessage(Language.GAME_ALREADY_STARTED);
            return false;
        }
        if (gameStage == GameStage.END) {
            player.sendMessage(Language.GAME_ALREADY_ENDED);
            return false;
        }
        if (!isEmptyInventory(player) && !player.isOp()) {
            player.sendMessage(Language.CLEAR_INVENTORY);
            return false;
        }
        if (WoodenMan.gameTask.addPlayer(player)) {
            player.sendMessage(Language.JOIN_GAME_SUCCESS);
            Bukkit.broadcastMessage(String.format(Language.PLAYER_JOIN_BROADCAST, player.getName())); // 使用 Language 中的常量
        } else {
            player.sendMessage(Language.JOIN_GAME_FAILED);
        }
        return true;
    }

    private boolean isEmptyInventory(Player player) {
        PlayerInventory inventory = player.getInventory();
        for (int i = 0; i < inventory.getSize(); i++) {
            ItemStack item = inventory.getItem(i);
            if (item == null) {
                continue; // 如果 item 为 null，跳过当前循环
            }
            if (item.getType() != Material.AIR) {
                return false; // 如果 item 不是空气，返回 false
            }
        }
        ItemStack[] armorContents = inventory.getArmorContents();
        if (armorContents != null) {
            for (ItemStack item : armorContents) {
                if (item == null) {
                    continue; // 如果 item 为 null，跳过当前循环
                }
                if (item.getType() != Material.AIR) {
                    return false; // 如果 item 不是空气，返回 false
                }
            }
        }
        ItemStack[] extraContents = inventory.getExtraContents();
        if (extraContents != null) {
            for (ItemStack item : extraContents) {
                if (item == null) {
                    continue; // 如果 item 为 null，跳过当前循环
                }
                if (item.getType() != Material.AIR) {
                    return false; // 如果 item 不是空气，返回 false
                }
            }
        }
        return true; // 如果所有 item 都是 null 或空气，返回 true
    }
}
